# -*- coding: utf-8 -*-

###################
#   miniTH
#   This program serves for serial change of photos size, to put watermarks
#   and at the same time for making thumbnails.
#   It can be useful for quick processing of big amount of pictures in order to put
#   them in web galleries or on websites.
#   Copyright (C) 2011 DooBLER
#
#   This program is free software: you can redistribute it and/or modify
#   it under the terms of the GNU General Public License as published by
#   the Free Software Foundation, either version 3 of the License, or any later version.
#
#   This program is distributed in the hope that it will be useful,
#   but WITHOUT ANY WARRANTY; without even the implied warranty of
#   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#   GNU General Public License for more details.
#
#   You should have received a copy of the GNU General Public License
#   along with this program.  If not, see <http://www.gnu.org/licenses/>.
###################

###################
# svn: $Id: My9BtnWidget.py 20 2011-08-15 17:40:39Z MrDooBLER@gmail.com $
# Author: DooBLER http://doobler.net
###################


from PySide import QtCore
from PySide import QtGui


class My9BtnWidget(QtGui.QWidget):

    valueChanged = QtCore.Signal(int)

    def __init__(self, parent):
        QtGui.QWidget.__init__(self, parent)

        self.value = 5

        # zdefiniowanie layoutów
        self.gridLayout = QtGui.QGridLayout()
        self.gridLayout.setHorizontalSpacing(2)
        self.gridLayout.setVerticalSpacing(1)

        self.alignLayout = QtGui.QHBoxLayout()
        self.valignLayout = QtGui.QVBoxLayout()

        # utworzenie 9 radio buttonów
        self.radio = [QtGui.QRadioButton() for e in range(9)]
        self.radio[4].setChecked(True)

        # pętla do masowej zmiany właściwości przycisków
        for q in self.radio:
            q.setSizePolicy(QtGui.QSizePolicy.Fixed, QtGui.QSizePolicy.Fixed)
            #q.setStyleSheet('background-color: green;')
            q.clicked.connect(self._btnChecked)

        #self.ui.obrUklRadio_1.toggled.connect(self._btnChecked)

        # dodanie przycisków do siatki
        self.gridLayout.addWidget(self.radio[0], 0, 0)
        self.gridLayout.addWidget(self.radio[1], 0, 1)
        self.gridLayout.addWidget(self.radio[2], 0, 2)

        self.gridLayout.addWidget(self.radio[3], 1, 0)
        self.gridLayout.addWidget(self.radio[4], 1, 1)
        self.gridLayout.addWidget(self.radio[5], 1, 2)

        self.gridLayout.addWidget(self.radio[6], 2, 0)
        self.gridLayout.addWidget(self.radio[7], 2, 1)
        self.gridLayout.addWidget(self.radio[8], 2, 2)

        

        # wyśrodkowanie przycisków w poziomie
        self.alignLayout.addStretch()
        self.alignLayout.addLayout(self.gridLayout)
        self.alignLayout.addStretch()

        # przesunięcie do góry
        self.valignLayout.addLayout(self.alignLayout)
        self.valignLayout.addStretch()

        self.setLayout(self.valignLayout)





    def _btnChecked(self):

        for id,val in enumerate(self.radio):
            if val.isChecked():
                self.value = id+1
                break

        self.valueChanged.emit(self.value)



    def setValue(self, val):
        if 0 <= val <= 9:
            # wyłączenie aktywnego buttonu
            if self.value > 0:
                self.radio[self.value-1].setChecked(False)
            # włączenie wybranego buttonu
            if val > 0:
                self.radio[val-1].setChecked(True)
            self.value = val
        else:
            # value error
            pass


    def getValue(self):
        return self.value;

        